package com.manager.service.impl;

import com.manager.dao.UserMapper;
import com.manager.entity.User;
import com.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;/*改成public？？？*/


    @Override
    public String verify(String userid, String password, int identity, boolean online) {
        User user = userMapper.selectByPrimaryKey(userid);/*非空。*/
        if (user == null) {/*输入检测*/
            return "false";
        }
        if (password.equals(user.getPassword()) && user.getIdentity() == identity) {
            if (user.getIdentity()==1){/*管理员*/
                return "administrator";
            }else {
                return "true";
            }
        }
        return "false";
    }

    @Override
    public String register(String userid, String password, int identity) {
        User user = new User(userid, password, identity);
        int i = 0;
        try {
            i = userMapper.insert(user);
        }catch (DuplicateKeyException e){
            return "DuplicateKeyException";
        }catch (Exception e) {
            e.printStackTrace();
            return "Exception";
        }
        return "success:"+i;
    }
}